﻿using System;
using System.Reflection;
using System.Collections.Generic;
using NLog;
using dlCentral.Utils;
using dlCentral.PluginHandlers;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace dlCentral.TorrentHandlers {
    internal class IsoHuntHandler : TorrentHandler {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        

        // The torrent ID for the plugin this handler implements support for.
        public override TorrentId ID
        {
            get { return TorrentId.ISOHUNT; }
            set { }
        }

        // Should return the name of the source Torrent providing the data.
        public override string TorrentName {
            get { return "IsoHunt"; }
            set { }
        }

        public override void GetDetails(List<MediaDetail> Results)
        {
            url = "http://isohunt.com/js/json.php?ihq={0}&start=1&rows=50&sort=seeds";
            if (DLSType == SiteSearchType.IMDb) Category = "Video/Movies";
            if (DLSType == SiteSearchType.TVSHOW) Category = "TV";
            JObject jobject = new JObject();
            string size = "";
            int seeds = 0;
            int peers = 0;
            int files = 0;
            string date = "";
            url = string.Format(url, SearchTerm); 
            string json = get(url);
            try
            {
                jobject = JObject.Parse(json);
            }
            catch
            {
                return;
            }

            JArray ja = (JArray)jobject.SelectToken("items.list");
            if (ja != null)
            foreach (JObject jo in ja)
            {
                string cat = (string)(jo.SelectToken("category"));
                if (cat != Category) continue;
                MediaDetail item = new MediaDetail();
                item.Name = (string)(jo.SelectToken("title"));
                item.TorrentURL = (string)(jo.SelectToken("enclosure_url"));
                                            
                size = (string)(jo.SelectToken("size"));
                try
                {
                    seeds = (int)(jo.SelectToken("Seeds"));
                }
                catch { seeds = 0; }
                try
                {
                    files = (int)(jo.SelectToken("files"));
                }
                catch { files = 0; }
                try
                {
                    peers = (int)(jo.SelectToken("leechers"));
                }
                catch { peers = 0; }

                date = (string)(jo.SelectToken("pubDate"));
                item.Date = DateTime.Parse(date);

                item.Name = item.Name.Replace("<b>", "");
                item.Name = item.Name.Replace("</b>", "");
                if (DLSType == SiteSearchType.TVSHOW) item.IsInfoChecked = true;
                item.TorrentInfo = "NrOfFiles : " + Convert.ToString(files) + "   " + size + " S/P : " + Convert.ToString(seeds) + "/" + Convert.ToString(peers);
                item.SearchType = DLSType;
                item.Site = Site;
                Results.Add(item);
            }
        }

        protected override bool IsAvailable()
        {
            return true;
        }


    }
}
